package com.jintoufs.web.action.images;

import com.jintoufs.domain.images.Images;
import com.jintoufs.domain.user.User;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.hotel.HotelRoomService;
import com.jintoufs.service.image.ImageService;
import com.jintoufs.service.scene.SceneTicketService;
import com.jintoufs.service.tour.TourRouteService;
import com.jintoufs.service.user.UserService;
import com.jintoufs.util.http.HttpRequest;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/images")
public class ImageAction extends SupportAction {

    @Resource
    private ImageService imageService;
    @Resource
    private UserService userService;
    @Resource
    private HotelRoomService hotelRoomService;
    @Resource
    private SceneTicketService sceneTicketService;
    @Resource
    private TourRouteService tourRouteService;

    @RequestMapping("/setImages")
    @SystemControllerLog(description = "设置图册")
    public String images(String valueId, Model model, @CurrentUser User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("valueId", valueId);
//        param.put("customerId",user.getId());
        List<Images> imagesList = imageService.selectByparams(param);
        model.addAttribute("images", imagesList);
        model.addAttribute("valueId", valueId);
        return "/images/images";
    }

    @RequestMapping("/addImages")
    public AjaxMsg addImage(String valueId, @RequestParam("file[]") CommonsMultipartFile[] files, @CurrentUser User user) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            Images images;
            boolean cover = true;

            List<Map<String, Object>> maps;
            Map<String, Object> param = new HashMap<>();
            param.clear();
            param.put("cover", "1");
            param.put("valueId", valueId);
            if (imageService.selectByparams(param).size() > 0) {
                cover = false;
            }
            param.clear();
            param.put("id", valueId);
            maps = hotelRoomService.selectByParams(param);
            if (maps.size() > 0) {
                param.clear();
                param.put("valueId", maps.get(0).get("hotelId"));
            } else {
                maps = sceneTicketService.queryByParams(param);
                if (maps.size() > 0) {
                    param.clear();
                    param.put("valueId", maps.get(0).get("sceneId"));
                } else {
                    maps = tourRouteService.queryByParams(param);
                    if (maps.size() > 0) {
                        param.clear();
                        param.put("valueId", maps.get(0).get("travelAgencyId"));
                    } else {
                        param.clear();
                        param.put("valueId", valueId);
                    }
                }
            }
            param.put("customerId", user.getId());
            if (userService.auth(param)) {
                String attachment = imageService.upToCos(files);
                String[] pathList = attachment.split(",");
                for (String path : pathList) {
                    if (!path.contains("error")) {
                        images = new Images();
                        images.setId(getUUID());
                        images.setPath(path);
                        images.setCreateTime(new Date());
                        images.setValueId(valueId);
                        if (cover) {
                            images.setCover("1");
                        }
                        imageService.insert(images);
                    }
                    cover = false;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("系统出错，可能有部分未上传完成请刷新后重试。");
        }
        return ajaxMsg;
    }

    @RequestMapping("delete")
    @ResponseBody
    public AjaxMsg delete(String id) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            imageService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode("500");
        }
        return ajaxMsg;
    }

    @RequestMapping("getImages")
    @ResponseBody
    public AjaxMsg getImages(String valueId) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            Map map = new HashMap();
            map.put("valueId", valueId);
            List<Images> list=imageService.selectByparams(map);
            ajaxMsg.setDatas(list);
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("查询图片失败");
        }
        return ajaxMsg;
    }

}
